﻿package mortal.game.view.group.groupAvatar
{
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.game.view.group.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GroupAvatarController extends Controller
    {
        private var _groupAvatarView:GroupAvatarView;
        private var _groupCahce:GroupCache;

        public function GroupAvatarController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._groupCahce = Cache.instance.group;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._groupAvatarView == null)
            {
                this._groupAvatarView = new GroupAvatarView();
                this._groupAvatarView.addEventListener(WindowEvent.SHOW, this.onGroupShow);
                this._groupAvatarView.addEventListener(WindowEvent.CLOSE, this.onGroupClose);
            }
            return this._groupAvatarView;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GroupPlayerInfoChange, this.updateGroupAvatar);
            NetDispatcher.addCmdListener(ServerCommand.TeamMateLeave, this.teamMateLeave);
            NetDispatcher.addCmdListener(ServerCommand.updateTeamMateState, this.updateTeamMateState);
            Dispatcher.addEventListener(EventName.SceneAddGroupmate, this.teamMateIn);
            Dispatcher.addEventListener(EventName.SceneRemoveGroupmate, this.teamMateOut);
            Dispatcher.addEventListener(EventName.GroupAvatarVisible, this.onGroupAvatarVisible);
            Dispatcher.addEventListener(EventName.ChatTeamTalk, this.addTeamTalk);
            return;
        }// end function

        private function onGroupShow(event:Event) : void
        {
            return;
        }// end function

        private function onGroupClose(event:Event) : void
        {
            return;
        }// end function

        private function addTeamTalk(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2.entityId;
            var _loc_4:* = _loc_2.content;
            if (EntityUtil.isSelf(_loc_3))
            {
                return;
            }
            if (this.isViewShow)
            {
                this._groupAvatarView.addChatTxtByID(_loc_3, _loc_4);
            }
            return;
        }// end function

        private function updateGroupAvatar(param1:Object = null) : void
        {
            var _loc_2:Boolean = false;
            if (this._groupCahce.isInGroup)
            {
                if (!this.isViewShow)
                {
                    GameManager.instance.popupModuleType(ModuleType.GroupAvatar);
                    BuffUtil.addBuffByCode(23000041);
                }
                this._groupAvatarView.updateTeamMate();
            }
            else
            {
                if (this._groupAvatarView)
                {
                    this._groupAvatarView.hide();
                }
                BuffUtil.removeBuffByCode(23000041);
            }
            if (Cache.instance.group.isInCopyGroup && !CopyUtil.isInCopy)
            {
                _loc_2 = Cache.instance.group.isTeamMateInCopy;
                if (!_loc_2)
                {
                    GroupCopyWaitingView.instance.show();
                }
                else if (GroupCopyWaitingView.isViewShow)
                {
                    GroupCopyWaitingView.instance.hide();
                }
            }
            else if (GroupCopyWaitingView.isViewShow)
            {
                GroupCopyWaitingView.instance.hide();
            }
            return;
        }// end function

        private function teamMateIn(event:DataEvent) : void
        {
            var _loc_2:* = event.data as EntityInfo;
            if (this.isViewShow)
            {
                this._groupAvatarView.teamMateIn(_loc_2);
            }
            return;
        }// end function

        private function teamMateOut(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (this.isViewShow)
            {
                this._groupAvatarView.teamMateOut(_loc_2);
            }
            return;
        }// end function

        private function onGroupAvatarVisible(event:DataEvent) : void
        {
            if (this.isViewShow)
            {
                this._groupAvatarView.showList = event.data;
            }
            return;
        }// end function

        private function teamMateLeave(param1:Object) : void
        {
            var _loc_2:* = param1 as SEntityId;
            if (this.isViewShow)
            {
                this._groupAvatarView.removeTeamMate(_loc_2);
            }
            return;
        }// end function

        private function updateTeamMateState(param1:Object) : void
        {
            if (this.isViewShow)
            {
                this._groupAvatarView.updateTeamMateState(param1);
            }
            return;
        }// end function

    }
}
